home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / http / httpdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  9.2 KB  |  332 lines

  1. // HttpDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. #include "Http.h"
  7. #include "HttpDlg.h"
  8.  
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CHttpApp
  18.  
  19. BEGIN_MESSAGE_MAP(CHttpApp, CWinApp)
  20.     //{{AFX_MSG_MAP(CHttpApp)
  21.         // NOTE - the ClassWizard will add and remove mapping macros here.
  22.         //    DO NOT EDIT what you see in these blocks of generated code!
  23.     //}}AFX_MSG
  24. END_MESSAGE_MAP()
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CHttpApp construction
  28.  
  29. CHttpApp::CHttpApp(LPCTSTR lpszAppName)
  30.     : CWinApp(lpszAppName)
  31. {
  32. }
  33.  
  34. /////////////////////////////////////////////////////////////////////////////
  35. // The one and only CHttpApp object
  36.  
  37. // WCE MFC apps require the application name to be specified in the CWinApp 
  38. // constructor. A help contents filename may also be specified.
  39.  
  40. CHttpApp theApp(_T("Http Sample"));
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. // CHttpApp initialization
  44.  
  45. BOOL CHttpApp::InitInstance()
  46. {
  47.     CHttpDlg dlg;
  48.     m_pMainWnd = &dlg;
  49.     int nResponse = dlg.DoModal();
  50.  
  51.     return FALSE;
  52. }
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CHttpSession object
  57.  
  58. // Http wants to use its own derivative of the CHttpSession class
  59. // just so it can implement an OnStatusCallback() override.
  60.  
  61. CMyHttpSession::CMyHttpSession(LPCTSTR pszAppName, DWORD dwContext, int nMethod)
  62.     : CInternetSession(pszAppName, dwContext, nMethod)
  63. {
  64. }
  65.  
  66. void CMyHttpSession::OnStatusCallback(DWORD dwContext, DWORD dwInternetStatus,
  67.     LPVOID /* lpvStatusInfomration */, DWORD /* dwStatusInformationLen */)
  68. {
  69.     CHttpDlg* pDlg = (CHttpDlg*)dwContext;
  70.     if(!pDlg)
  71.         return;
  72.  
  73.     switch(dwInternetStatus)
  74.     {
  75.         case INTERNET_STATUS_RESOLVING_NAME: 
  76.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_IPLOOKING));
  77.             break;
  78.         case INTERNET_STATUS_NAME_RESOLVED: 
  79.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_IPFOUND));
  80.             break;
  81.         case INTERNET_STATUS_CONNECTING_TO_SERVER: 
  82.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTING));
  83.             break;
  84.         case INTERNET_STATUS_CONNECTED_TO_SERVER: 
  85.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTED));
  86.             break;
  87.         case INTERNET_STATUS_SENDING_REQUEST: 
  88.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_REQUEST));
  89.             break;
  90.         case INTERNET_STATUS_REQUEST_SENT: 
  91.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_SENT));
  92.             break;
  93.         case INTERNET_STATUS_RECEIVING_RESPONSE: 
  94.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_WAITING));
  95.             break;
  96.         case INTERNET_STATUS_RESPONSE_RECEIVED: 
  97.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_RECEIVED));
  98.             break;
  99.         case INTERNET_STATUS_CLOSING_CONNECTION: 
  100.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CLOSING));
  101.             break;
  102.         case INTERNET_STATUS_CONNECTION_CLOSED: 
  103.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CLOSED));
  104.             break;
  105.         case INTERNET_STATUS_HANDLE_CREATED: 
  106.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_CONNECTED2));
  107.             break;
  108.         case INTERNET_STATUS_HANDLE_CLOSING: 
  109.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_DISCONNECTED));
  110.             break;
  111.         case INTERNET_STATUS_REQUEST_COMPLETE: 
  112.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_COMPLETED));
  113.             break;
  114.         case INTERNET_STATUS_REDIRECT: 
  115.             pDlg->SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_REDIRECTED));
  116.             break;
  117.     }
  118. }
  119.  
  120. void ThrowHttpException(int nCode)
  121. {
  122.     CInternetException* pEx = new CInternetException(nCode);
  123.     THROW(pEx);
  124. }
  125.  
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CHttpDlg dialog
  129.  
  130. CHttpDlg::CHttpDlg(CWnd* pParent /*=NULL*/)
  131.     : CDialog(CHttpDlg::IDD, pParent)
  132. {
  133.     //{{AFX_DATA_INIT(CHttpDlg)
  134.         // NOTE: the ClassWizard will add member initialization here
  135.     //}}AFX_DATA_INIT
  136.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  137.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  138. }
  139.  
  140. void CHttpDlg::DoDataExchange(CDataExchange* pDX)
  141. {
  142.     CDialog::DoDataExchange(pDX);
  143.     //{{AFX_DATA_MAP(CHttpDlg)
  144.         // NOTE: the ClassWizard will add DDX and DDV calls here
  145.     //}}AFX_DATA_MAP
  146. }
  147.  
  148. BEGIN_MESSAGE_MAP(CHttpDlg, CDialog)
  149.     //{{AFX_MSG_MAP(CHttpDlg)
  150.     ON_BN_CLICKED(ID_DOWNLOADPAGE, OnDownloadpage)
  151.     //}}AFX_MSG_MAP
  152. END_MESSAGE_MAP()
  153.  
  154. /////////////////////////////////////////////////////////////////////////////
  155. // CHttpDlg message handlers
  156.  
  157. BOOL CHttpDlg::OnInitDialog()
  158. {
  159.     CDialog::OnInitDialog();
  160.  
  161.     // Set the icon for this dialog.  The framework does this automatically
  162.     //  when the application's main window is not a dialog
  163.     SetIcon(m_hIcon, TRUE);            // Set big icon
  164.     SetIcon(m_hIcon, FALSE);        // Set small icon
  165.     
  166.     CenterWindow(GetDesktopWindow());    // center to the hpc screen
  167.  
  168.     SetDlgItemText(IDC_ADDRESS, CString((LPCTSTR)IDS_DEFAULT));
  169.     
  170.     return TRUE;  // return TRUE  unless you set the focus to a control
  171. }
  172.  
  173. void CHttpDlg::OnDownloadpage() 
  174. {
  175.     TCHAR szAddress[INTERNET_MAX_HOST_NAME_LENGTH];
  176.     GetDlgItemText(IDC_ADDRESS, szAddress, INTERNET_MAX_HOST_NAME_LENGTH - 1);
  177.  
  178.     SetDlgItemText(IDC_MESSAGE, _T(""));
  179.     SetDlgItemText(IDC_EDIT_PAGE, _T(""));
  180.  
  181.     DownloadPage(szAddress);
  182. }
  183.  
  184. int CHttpDlg::DownloadPage(LPTSTR szAddress)
  185. {
  186.     int   nRetCode = 0;
  187.     DWORD dwAccessType = PRE_CONFIG_INTERNET_ACCESS;
  188.     const TCHAR szHeaders[] = _T("Accept: text/*\r\nUser-Agent: MFC_Http_Sample\r\n");
  189.     DWORD dwHttpRequestFlags = INTERNET_FLAG_NO_AUTO_REDIRECT;
  190.     BOOL  bSuccess = TRUE;
  191.  
  192.     CMyHttpSession session(CString((LPCTSTR)IDS_HTTPAPP), (DWORD)this, dwAccessType);
  193.     CHttpConnection* pServer = NULL;
  194.     CHttpFile* pFile = NULL;
  195.     TRY
  196.     {
  197.         // check to see if this is a reasonable URL
  198.         CString strServerName;
  199.         CString strObject;
  200.         INTERNET_PORT nPort;
  201.         DWORD dwServiceType;
  202.  
  203.         if (!AfxParseURL(szAddress, dwServiceType, strServerName, strObject, nPort) || 
  204.             dwServiceType != INTERNET_SERVICE_HTTP)
  205.         {
  206.             SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_ERROR1));
  207.             ThrowHttpException(1);
  208.         }
  209.  
  210.         session.EnableStatusCallback(TRUE);
  211.  
  212.         pServer = session.GetHttpConnection(strServerName, nPort);
  213.  
  214.         pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET, strObject, NULL, (DWORD)this, NULL, NULL, dwHttpRequestFlags);
  215.         pFile->AddRequestHeaders(szHeaders);
  216.         pFile->SendRequest();
  217.  
  218.         DWORD dwRet;
  219.         pFile->QueryInfoStatusCode(dwRet);
  220.  
  221.         if (dwRet == HTTP_STATUS_DENIED)
  222.             return 0;
  223.  
  224.         CString strNewLocation;
  225.         pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
  226.  
  227.         // were we redirected?
  228.         // these response status codes come from WININET.H
  229.  
  230.         if (dwRet == HTTP_STATUS_MOVED ||
  231.             dwRet == HTTP_STATUS_REDIRECT ||
  232.             dwRet == HTTP_STATUS_REDIRECT_METHOD)
  233.         {
  234.             CString strNewLocation;
  235.             pFile->QueryInfo(HTTP_QUERY_RAW_HEADERS_CRLF, strNewLocation);
  236.  
  237.             int nPlace = strNewLocation.Find(_T("Location: "));
  238.             if (nPlace == -1)
  239.             {
  240.                 SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_ERROR2) );
  241.                 ThrowHttpException(2);
  242.             }
  243.  
  244.             strNewLocation = strNewLocation.Mid(nPlace + 10);
  245.             nPlace = strNewLocation.Find('\n');
  246.             if (nPlace > 0)
  247.                 strNewLocation = strNewLocation.Left(nPlace);
  248.  
  249.             // close up the redirected site
  250.  
  251.             pFile->Close();
  252.             delete pFile;
  253.             pServer->Close();
  254.             delete pServer;
  255.  
  256.             CString csMsg = CString((LPCTSTR)IDS_CAUTION) + strNewLocation;
  257.             SetDlgItemText(IDC_MESSAGE, csMsg);
  258.  
  259.             // figure out what the old place was
  260.             if (!AfxParseURL(strNewLocation, dwServiceType, strServerName, strObject, nPort))
  261.             {
  262.                 SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_ERROR3));
  263.                 ThrowHttpException(2);
  264.             }
  265.  
  266.             if (dwServiceType != INTERNET_SERVICE_HTTP)
  267.             {
  268.                 SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_ERROR4));
  269.                 ThrowHttpException(2);
  270.             }
  271.  
  272.             // try again at the new location
  273.             pServer = session.GetHttpConnection(strServerName, nPort);
  274.             pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_GET,
  275.                 strObject, NULL, (DWORD)this, NULL, NULL, dwHttpRequestFlags);
  276.             pFile->AddRequestHeaders(szHeaders);
  277.             pFile->SendRequest();
  278.  
  279.             pFile->QueryInfoStatusCode(dwRet);
  280.             if (dwRet != HTTP_STATUS_OK)
  281.                 ThrowHttpException(2);
  282.         }
  283.  
  284.         TCHAR* szWEBPage = new TCHAR[MAX_WEBPAGE_SIZE];
  285.         if(szWEBPage)
  286.         {
  287.             szWEBPage[0] = L'\0';
  288.  
  289.             TCHAR* sz     = new TCHAR[BUFFER_SIZE];
  290.             TCHAR* szwBuf = new TCHAR[BUFFER_SIZE * 2];
  291.             sz[0] = L'\0';
  292.             int n = 0;
  293.             while (pFile->ReadString(sz, BUFFER_SIZE))
  294.             {
  295.                 wce_AsciiToWide(szwBuf, (const char*)sz);
  296.  
  297.                 n += _tcslen(szwBuf);
  298.                 if(n >= MAX_WEBPAGE_SIZE)
  299.                     break;
  300.                 _tcscat(szWEBPage, szwBuf);
  301.             }
  302.             delete [] sz;
  303.             delete [] szwBuf;
  304.             SetDlgItemText(IDC_EDIT_PAGE, szWEBPage);
  305.             delete [] szWEBPage;
  306.         }
  307.  
  308.         pFile->Close();
  309.         pServer->Close();
  310.     }
  311.     CATCH (CInternetException,  pEx)
  312.     {
  313.         // catch things wrong with parameters, etc
  314.         SetDlgItemText(IDC_EDIT_PAGE, CString((LPCTSTR)IDS_EXCEPTION));
  315.         pEx->Delete();
  316.         bSuccess = FALSE;
  317.     }
  318.     END_CATCH_ALL
  319.  
  320.     if (pFile != NULL)
  321.         delete pFile;
  322.     if (pServer != NULL)
  323.         delete pServer;
  324.     session.Close();
  325.  
  326.     if(bSuccess)
  327.         SetDlgItemText(IDC_MESSAGE, CString((LPCTSTR)IDS_DOWNLOADED));
  328.  
  329.     return nRetCode;
  330. }
  331.  
  332.